<?php
class item_decrease_own_withdraw extends template_item
{
	var $default_name = "Decrease the Withdraw Fee";
	var $default_description = "Decrease the withdraw fee of one of your bank accounts";
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['rate'] = 1;
			$this->item_config['min_rate'] = 0;
		}
	}
	
	function admin_form()
	{
		if($this->ipsclass->market_class->count_plugin_filename("bank.php") == 0)
		{
			$this->ipsclass->html = "";
			$this->ipsclass->admin->error("The bank plugin is not installed");
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Decrease the withdraw fee by how much?</b>",
															$this->ipsclass->adskin->form_simple_input("item[".$this->item['item_id']."][rate]", $this->item_config['rate'])."%",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What is the minimum withdraw fee?</b>",
															$this->ipsclass->adskin->form_simple_input("item[".$this->item['item_id']."][min_rate]", $this->item_config['min_rate'])."%",
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		if($this->ipsclass->market_class->count_plugin_filename("bank.php") == 0)
		{
			$this->ipsclass->html = "";
			$this->ipsclass->admin->error("The bank plugin is not installed");
		}
		
		$save = array();
		$save['rate'] = $this->ipsclass->numbers->parse_input_number($input['rate']);
		$save['min_rate'] = $this->ipsclass->numbers->parse_input_number($input['min_rate']);
		if($this->ipsclass->numbers->compare($save['min_rate'],0) < 0)
		{
			$save['min_rate'] = 0;
		}
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		if(is_array($this->ipsclass->cache['market_plugin']) && count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['filename'] == "bank.php")
				{
					$bank_info = $plugin;
				}
			}
		}
		
		$this->ipsclass->load_language('lang_market_plugin');
		
		$bank = $this->ipsclass->market_class->load_plugin($bank_info);
		
		$accounts = "";
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"a_mem_id=".$this->ipsclass->member['id']));
		$this->ipsclass->DB->exec_query();
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->Error(array('MSG'=>'bank_no_account'));
		}
		while($account = $this->ipsclass->DB->fetch_row())
		{
			$accounts .= "					<option value='".$bank->_build_bank_account_id($account['account_id'],$account['a_random'],$account['a_mem_id'])."'>".$bank->_build_bank_account_id($account['account_id'],$account['a_random'],$account['a_mem_id'])."</option>\n";
		}
		
		$rows = "";
		foreach($inventory_ids as $inv_id)
		{
			$rows .= <<<EOF
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['bank_which_account']}</b>
			</td>
			<td class='row2' align='right'>
				<select name='bank_account[{$inv_id}]'>
{$accounts}
				</select>
			</td>
		</tr>
EOF;
		}
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['bank_decrease_withdraw']}</div>
<form action='{$this->base_url}' method='post' enctype='multipart/form-data' id='changeform'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
{$rows}
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' value='{$this->ipsclass->lang['bank_decrease_withdraw_rate']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done($inventory_ids = array())
	{
		if(!isset($this->ipsclass->input['bank_account']))
		{
			return false;
		}
		foreach($inventory_ids as $inv_id)
		{
			if(!isset($this->ipsclass->input['bank_account'][$inv_id]))
			{
				return false;
			}
		}
		return true;
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		if(is_array($this->ipsclass->cache['market_plugin']) && count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['filename'] == "bank.php")
				{
					$bank_info = $plugin;
				}
			}
		}
		
		$this->ipsclass->load_language('lang_market_plugin');
		
		$bank = $this->ipsclass->market_class->load_plugin($bank_info);
		
		$delete = array();
		foreach($inventory_ids as $inv_id)
		{
			$account_info = $bank->_revert_id($this->ipsclass->input['bank_account'][$inv_id]);
			$account = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$account_info['account']));
			if(!$account || $account['a_mem_id'] != $account_info['member'] || $account['a_mem_id'] != $this->ipsclass->member['id'] || $account['a_random'] != $account_info['random'])
			{
				continue;
			}
			
			$new_fee = $this->ipsclass->numbers->subtract($account['a_withdraw_fee'],$this->item_config['rate']);
			if($this->ipsclass->numbers->compare($new_fee,$this->item_config['min_rate']) < 0)
			{
				$new_fee = $this->item_config['min_rate'];
			}
			if($this->ipsclass->numbers->compare($new_fee,$account['a_withdraw_fee']) >= 0)
			{
				continue;
			}
			
			$this->ipsclass->DB->do_update('market_bank_accounts',array('a_withdraw_fee'=>$new_fee),"account_id=".$account_info['account']);
			
			$delete[] = $inv_id;
			$this->_write_log($inv_id, "Bank account, ".$this->ipsclass->input['bank_account'][$inv_id].", decrease withdraw fee to ".$new_fee."%");
		}
		
		$this->delete_items($delete);
		
		$this->ipsclass->market_class->redirect('bank_withdraw_decreased',"plugin=".$bank_info['link']);
	}
}
?>